import sched
import time
from pkgutil import get_data
from  PyQt5.QtCore import QThread,pyqtSignal,QMutex,QTimer,QObject
from model.robot_history_model import RobotHistoryModel
from model.user_model import UserModel

class HomeService:
    def __init__(self):
        pass
    @staticmethod
    def get_qty():#获取合计数据

        qty = RobotHistoryModel.get_act_qty_by_now()
        resume_qty = 0
        msg_qty = 0
        i = 0
        for i in range(len(qty)):
            if qty[i]['robot_act']==0:
                resume_qty = qty[i]['act_qty']
            else:
                if qty[i]['robot_act'] ==2:
                    msg_qty = qty[i]['act_qty']
        robot_qty = UserModel.get_qty_by_states(1)
        work_robot_qty = robot_qty['qty']
        return  work_robot_qty,resume_qty, msg_qty


class GetQtyWorker(QObject):  #定时取数工作类
    update_signal = pyqtSignal(int,int,int)  # 定义一个信号，用于更新UI

    def __init__(self):
        super().__init__()
        self.timer = QTimer(self)

    def start_work(self):
        # 确保 QTimer 绑定到当前线程的事件循环
        self.timer.timeout.connect(self.get_qty_work)
        self.timer.setInterval(3000)#1秒触发一次
        self.timer.start()

    def get_qty_work(self):
        # 这里是定时执行的任务
        work_robot_qty, resume_qty, msg_qty =HomeService.get_qty()
        current_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.update_signal.emit(work_robot_qty,resume_qty,msg_qty)


# class GetDataSched():
#     def __init__(self):
#         self. work_robot_qty = 0
#         self.resume_qty = 0
#         self.msg_qty = 0
#     def get_qty(self):
#         self.work_robot_qty, self.resume_qty, self.msg_qty = HomeService.get_qty()
#         # print([self.work_robot_qty, self.resume_qty, self.msg_qty])
#     def get_data_scheduler(self):
#         s=sched.scheduler(time.time, time.sleep)
#         s.enter(0,1,self.get_qty,())
#         s.enter(2,1,self.get_data_scheduler,())
#         s.run()




